package com.jiulan.hearthearing.common;

import android.content.Context;
import android.content.SharedPreferences;

import com.jiulan.hearthearing.base.MyApp;
import com.jiulan.hearthearing.ui.my.model.UserInfoModel;
import com.jiulan.hearthearing.utils.StringUtils;

public class UserData {
    private Context context;
    private static UserData UD = null;//会员业务逻辑类 对象
    private final String USER_FILE_NAME = "user_info";//会员信息文件名
    private final String LOGINSTATE = "islogin";

    public static final String UID = "uid";//用户id
    public static final String REALNAME = "real_name";//真实姓名
    public static final String NICKNAME = "nickname";//昵称
    public static final String AVATAR = "avatar";//头像地址
    public static final String PHONE = "phone";
    public static final String YUE = "now_money";
    public static final String INTEGRAL = "integral";
    public static final String SEX = "sex";
    public static final String CARDID = "card_id";
    public static final String PERSONSIGN = "person_sign";
    public static final String SCHOOL = "school";
    public static final String PROFESSION = "profession";
    public static final String WORK = "work";
    public static final String BIRTHDAY = "birthday";

    private UserData(Context context) {
        this.context = context;
    }

    /**
     * 单利模式
     *
     * @param context
     * @return
     */
    public static synchronized UserData create(Context context) {
        return create();
    }

    /**
     * 单利模式
     *
     * @return
     */
    public static synchronized UserData create() {
        if (UD == null)
            UD = new UserData(MyApp.context);
        return UD;
    }

    /**
     * 是否登陆
     *
     * @return
     */
    public boolean isLogin() {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        if (sp.contains(LOGINSTATE)) {
            return sp.getBoolean(LOGINSTATE, false);
        } else {
            return false;
        }
    }

    /**
     * 改变登陆状态  清空保存数据
     *
     * @param islogin
     * @return
     */
    public UserData ChangeLoginState(boolean islogin) {
        //退出登陆
        if (!islogin) {
            clearAll();
        }
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.putBoolean(LOGINSTATE, islogin);
        ed.commit();
        return UD;
    }

    private void clearAll() {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.clear();
        ed.commit();
    }

    /**
     * 获取数据(String类型)
     *
     * @param key
     * @return
     */
    public String get(String key) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getString(key, null);
    }

    /**
     * 获取数据(int类型)
     *
     * @param key
     * @return
     */
    public int get(String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    /**
     * 获取数据(long类型)
     *
     * @param key
     * @return
     */
    public long getL(String key, long defValue) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getLong(key, defValue);
    }

    /**
     * 保存数据
     *
     * @param userBean
     * @return
     */
    public boolean saveUserData(UserInfoModel.DataBean userBean) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putInt(UID, userBean.getUid());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ed.putString(REALNAME, StringUtils.getString(userBean.getReal_name()));
        } catch (Exception e) {
            ed.putString(REALNAME, "");
        }

        try {
            ed.putString(NICKNAME, StringUtils.getString(userBean.getNickname()));
        } catch (Exception e) {
            ed.putString(NICKNAME, "");
        }

        try {
            ed.putString(AVATAR, StringUtils.getString(userBean.getAvatar()));
        } catch (Exception e) {
            ed.putString(AVATAR, "");
        }

        try {
            ed.putString(PHONE, StringUtils.getString(userBean.getPhone()));
        } catch (Exception e) {
            ed.putString(PHONE, "");
        }

        try {
            ed.putString(YUE, StringUtils.getString(userBean.getNow_money()));
        } catch (Exception e) {
            ed.putString(YUE, "");
        }

        try {
            ed.putString(INTEGRAL, StringUtils.getString(userBean.getIntegral()));
        } catch (Exception e) {
            ed.putString(INTEGRAL, "");
        }

        try {
            ed.putInt(SEX, userBean.getSex());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ed.putString(CARDID, StringUtils.getString(userBean.getCard_id()));
        } catch (Exception e) {
            ed.putString(CARDID, "");
        }

        try {
            ed.putString(PERSONSIGN, StringUtils.getString(userBean.getPerson_sign()));
        } catch (Exception e) {
            ed.putString(PERSONSIGN, "");
        }

        try {
            ed.putString(SCHOOL, StringUtils.getString(userBean.getSchool()));
        } catch (Exception e) {
            ed.putString(SCHOOL, "");
        }

        try {
            ed.putString(PROFESSION, StringUtils.getString(userBean.getProfession()));
        } catch (Exception e) {
            ed.putString(PROFESSION, "");
        }

        try {
            ed.putString(WORK, StringUtils.getString(userBean.getWork()));
        } catch (Exception e) {
            ed.putString(WORK, "");
        }

        try {
            ed.putString(BIRTHDAY, StringUtils.getString(userBean.getBirthday()));
        } catch (Exception e) {
            ed.putString(BIRTHDAY, "");
        }

        ed.putBoolean(LOGINSTATE, true);

        return ed.commit();
    }

    /**
     * 改变数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean changeUserData(String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putString(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ed.commit();
    }

    /**
     * 改变数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean changeUserData(String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putInt(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ed.commit();
    }

    /**
     * 改变数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean changeUserData(String key, long value) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putLong(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ed.commit();
    }
}
